/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.texture;

import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.engine.Resource;
import com.badlogic.gdx.graphics.Texture;


public abstract class BaseManagedTexture extends EngineObject implements ManagedTexture {

    protected final Resource.Manager resourceManager = new Resource.Manager(this);

    protected boolean init;
    protected TextureDef def;
    protected Texture texture;
    protected TextureFrame textureFrame;
    protected SharedTexturedMesh sharedTexturedMesh;

    protected float invTexWidth;
    protected float invTexHeight;


    protected BaseManagedTexture(TextureDef def) {
        this.def = def;
    }

    @Override
    public abstract void init();

    protected boolean canDispose() {
        return init && resourceManager.canDispose();
    }

    @Override
    public String getKey() {
        return def.key;
    }

    @Override
    public TextureOptions getTextureOptions() {
        return def.textureOptions;
    }

    @Override
    public TextureDef getTextureDef() {
        return def;
    }

    @Override
    public void obtainRef(Consumer consumer) {
        resourceManager.obtainRef(consumer);
    }

    @Override
    public void releaseRef(Consumer consumer) {
        resourceManager.releaseRef(consumer);
    }

    @Override
    public void releaseAllRefs() {
        resourceManager.releaseAllRefs();
    }

    @Override
    public boolean hasRefs() {
        return resourceManager.hasRefs();
    }

    @Override
    public boolean isEagerDispose() {
        return resourceManager.isEagerDispose();
    }

    @Override
    public void setEagerDispose(boolean dispose) {
        resourceManager.setEagerDispose(dispose);
    }


    @Override
    public boolean equalResource(Resource otherResource) {
        return this == otherResource;
    }

    @Override
    public void markPersistent() {
        resourceManager.markPersistent();
    }

    @Override
    public void revokePersistent() {
        resourceManager.revokePersistent();
    }

    @Override
    public abstract void dispose();

    @Override
    public void bind() {
        if (texture == null)
            init();
        texture.bind();
    }

    @Override
    public int getWidth() {
        return getTexture().getWidth();
    }

    @Override
    public int getHeight() {
        return getTexture().getHeight();
    }

    @Override
    public TextureFrame setTextureFrame(final int x, final int y, final int width, final int height) {
        this.textureFrame.set(x, y, width, height);
        return this.textureFrame;
    }

    @Override
    public TextureFrame getTextureFrame() {
        return textureFrame;
    }

    @Override
    public SharedTexturedMesh getSharedTexturedMesh() {
        if (this.sharedTexturedMesh == null)
            this.sharedTexturedMesh = new SharedTexturedMesh(this);
        return sharedTexturedMesh;
    }

    @Override
    public TextureFrame createFullTextureFrame() {
        return new TextureFrameImpl(this);
    }

    @Override
    public Texture getTexture() {
        if (texture == null)
            throw new IllegalStateException("Texture not loaded yet.");
        return texture;
    }

    @Override
    public boolean isInit() {
        return init;
    }

    @Override
    public void register(StateListener listener) {
        this.resourceManager.register(listener);
    }

    @Override
    public void unregister(StateListener listener) {
        this.resourceManager.unregister(listener);
    }

    @Override
    public float getInvTexWidth() {
        return invTexWidth;
    }

    @Override
    public float getInvTexHeight() {
        return invTexHeight;
    }

}